<template>
  <el-dialog class="swiperDialog" :visible.sync="visible" :title="dataForm.id?'编辑':'新增'" width="600px" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form size="small" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="submit()" label-width="160px">
      <el-form-item label="轮播图片" prop="picture">
        <el-upload
          ref="pictureUpload"
          :action="uploadUrl"
          :headers="headers"
          :limit="1"
          list-type="picture-card"
          :file-list="dataForm.picture"
          accept=".jpeg,.png,.jpg,.gif"
          :on-success="successPictureHandle"
          :on-remove="removePictureHandle">
          <i class="el-icon-plus"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input class="s_input" v-model.number="dataForm.sort"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input class="s_input" v-model="dataForm.remarks"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button size="small" @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button size="small" type="primary" @click="submit()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import Cookies from 'js-cookie'
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      uploadUrl: window.SITE_CONFIG['apiURL'] + '/uploadfile',
      headers: { token: Cookies.get('token') },
      dataForm: {
        id: null,
        sort: null,
        remarks: null,
        picture: []
      }
    }
  },
  computed: {
    dataRule () {
      return {
        picture: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        sort: [
          { type: 'number', message: '该选项必须为数字值', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 显示弹窗
    init (row) {
      this.visible = true
      console.log(row)
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
        if (row) {
          this.getInfo(row)
        } else {
          this.resetForm()
        }
      })
    },
    // 图片上传成功
    successPictureHandle (response, file, fileList) {
      console.log(response, file, fileList)
      this.dataForm.picture.push(response.pictureInfoVOS[0])
      this.dataForm.picture = fileList.map(item => {
        if (item.response) {
          return {
            name: item.response.pictureInfoVOS[0].imgName,
            url: item.response.pictureInfoVOS[0].imgAddress
          }
        } else {
          return item
        }
      })
    },
    // 删除图片
    removePictureHandle (file, fileList) {
      console.log(file, fileList)
      this.dataForm.picture = fileList
    },
    resetForm () {
      this.dataForm = {
        id: null,
        sort: null,
        remarks: null,
        picture: []
      }
    },
    // 表单提交
    submit: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        let dataForm = JSON.parse(JSON.stringify(this.dataForm))
        dataForm.pictureUrl = dataForm.picture.map(item => item.url)[0]
        this.$http[this.dataForm.id ? 'put' : 'post']('/carlunbo', dataForm).then(({ data: res }) => {
          if (res.code === 0) {
            this.$message.success('保存成功!')
            this.visible = false
            this.$emit('refresh')
          } else {
            this.$message.error(res.msg)
          }
        }).catch((error) => {
          return this.$message.error(error)
        })
      })
    }, 1000, { 'leading': true, 'trailing': false }),
    // 获取轮播图信息
    getInfo (row) {
      this.dataForm = JSON.parse(JSON.stringify(row))
      this.dataForm.picture = [{
        url: this.dataForm.pictureUrl,
        name: this.dataForm.pictureUrl
      }]
    }
  }
}
</script>
<style lang="scss">
.swiperDialog{
  .el-dialog__header{
    .el-dialog__title{
      font-size: 14px;
      font-weight: bold;
    }
    border-bottom: 1px solid #f2f2f2;
  }
  .el-dialog__body{
    padding: 15px 20px;
  }
  .s_input{
    width: 80%;
    margin-right: 10px;
  }
  .s_picker{
    width: 37%;
  }
}
</style>
